use std::f64;

pub fn new_birthday_probability(n: u32) -> f64 {
    // 思路：按照生日悖论的计算方式，先计算所有人的生日都不相同的概率，计算时先取对数，最后取指数
    const YEAR_DAYS: u32 = 365;
    let rounded = |x: f64|{
        (x * 10000.0).round() / 10000.0
    };
    if n > YEAR_DAYS {
        return rounded(1.0);
    }
    let mut log_conflict_prob: f64 = 0.0;
    for i in 1..= n {
        log_conflict_prob += ((YEAR_DAYS - i + 1) as f64 ).ln() - (YEAR_DAYS as f64).ln();
    }
    let conflict_prob = log_conflict_prob.exp();
    rounded(1.0 - conflict_prob)
}
